﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace EfSandbox.Entities.Final.Common.Mappings {
    public class CurrencyMap : EntityTypeConfiguration<Currency> {
        public CurrencyMap() {
            // Table
            ToTable("fin.com_currencies");

            // Primary Key
            HasKey(c => c.CurrencyId);

            // Index
            Property(c => c.CurrencyCode)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_COM_CUR_CD", 1) { IsUnique = true }));

            // Attributes
            Property(c => c.CurrencyId)
                .HasColumnName("cur_id");

            Property(c => c.CurrencyCode)
                .IsRequired()
                .IsUnicode(false)
                .HasMaxLength(25)
                .HasColumnName("cur_cd");

            Property(c => c.CurrencyName)
                .IsOptional()
                .IsUnicode(false)
                .HasMaxLength(250)
                .HasColumnName("cur_desc");

            Property(c => c.CurrencyMinorUnit)
                .IsOptional()
                .HasColumnName("cur_minor_unit");

            Property(c => c.CurrencyNumericCode)
                .IsOptional()
                .HasColumnName("cur_numeric_cd");

            // Relationships
            // 0..1:n Country
            HasMany(c => c.Countries)
                .WithOptional(c => c.Currency)
                .HasForeignKey(c => c.CurrencyId);

        }

    }
}
